require 'View/UIBase/UIBase'

UISpecialDrawRecord = UIBase.New(PanelResNames.UISpecialDrawRecord)

function UISpecialDrawRecord.Init(func, id, type, notClose)
    UISpecialDrawRecord.activeId = id;
    UISpecialDrawRecord.pageType = type;
    UISpecialDrawRecord.notClose = notClose;
    UIBase.Init(UISpecialDrawRecord,func);
end

-- 创建回调
function UISpecialDrawRecord:OnCreate()
    self.countDownNum = 0;

    self.Text_RecordTips = self:GetChild("Text_RecordTips");
    self.Btn_Record = self:GetChild("Btn_Record");
    self.Btn_Record.text = getLanguage("OptionalPool_Btn_Record");
    self.Btn_Return = self:GetChild("Btn_Return");
    self.Btn_Return.text = getLanguage("Btn_Cancel");
    self.Loader_Cost = self:GetChild("Loader_Cost");
    self.Text_Num = self:GetChild("Text_Num");
    self.Text_Buy = self:GetChild("Text_Buy");
    self.List_RecordPage = self:GetChild("List_RecordPage");
    self.Text_Buy.text = getLanguage("OptionalPool_Btn_Buy")
    self.pageController = self:GetController("Page");
    self.HeadRectDrawItem = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    self:addEvent();
end

-- 显示
function UISpecialDrawRecord:OnEnable()
    if self.activeId == nil then
        self:OnBack();
    else
        self:Refresh();
    end
end

function UISpecialDrawRecord: addEvent()
    --self:RegisterOnClick(self:GetChild("Btn_BgClose"),self.OnClose);
    self:RegisterOnClick(self:GetChild("Btn_Return"),self.OnClose);
    self:RegisterOnClick(self:GetChild("Btn_Record"),self.OnRecord);
    self:RegisterOnClick(self:GetChild("Btn_Buy"),self.OnBuy);
    self.List_RecordPage.onClickItem:Add(self.ClickTag, self);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_SelectDrawSave,self.OnClose);
    self:RegisterEvent(Config.EventType.Fresh_SelectDrawBuy,self.OnClose);
    --self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
end

function UISpecialDrawRecord:removeEvent()
    self.List_RecordPage.onClickItem:Remove(self.ClickTag);
end

function UISpecialDrawRecord:Refresh()
    self:UpdateData();
end

function UISpecialDrawRecord:UpdateData()
    self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
    if self.tableData == nil or self.serverData == nil then
        self:OnBack();
        return ;
    end
    self.List_RecordPage:RemoveChildrenToPool();
    self.nowSelect = nil;
    if self.pageType == 1 then
        -- 购买
        self.pageController.selectedIndex = 1;
        self.Text_RecordTips.text = getLanguage("OptionalPool_SelectToBuy");
        self.buyNumList = {};
        for k, v in pairs(self.serverData.param.selectDrawBox) do
            table.insert(self.buyNumList, tonumber(k));
        end
        table.sort(self.buyNumList,function (a, b) return a < b end);
        for k = 1, #self.buyNumList do
            if self.nowSelect == nil then
                self.nowSelect = k;
            end
            local item = self.List_RecordPage:AddItemFromPool()
            self:updateItem(item, k, self.serverData.param.selectDrawBox[tostring(self.buyNumList[k])]);
        end
    else
        -- 纪录
        self.pageController.selectedIndex = 0;
        self.Text_RecordTips.text = getLanguage("OptionalPool_SelectSlot");
        self.nowSelect = 1;
        for i = 1, self.tableData.f_ResultSave do
            if self.serverData.param.selectDrawBox[tostring(i)] == nil then
                self.nowSelect = i;
                break;
            end
        end
        for i = 1, self.tableData.f_ResultSave do
            local item = self.List_RecordPage:AddItemFromPool()
            self:updateItem(item, i, self.serverData.param.selectDrawBox[tostring(i)]);
        end
    end
    self:updateCost();
end

function UISpecialDrawRecord:updateItem(itemObj, index, serverData)
    itemObj.name = index;
    local Text_Fields = itemObj:GetChild("Text_Fields");
    local List_Hero = itemObj:GetChild("List_Hero");
    local Text_Empty = itemObj:GetChild("Text_Empty");
    local typeController = itemObj:GetController("Type");
    Text_Empty.text = getLanguage("OptionalPool_EmptySlot")
    Text_Fields.text = index
    if serverData == nil or table.getn(serverData) == 0 then
        typeController.selectedIndex = 0;
    else
        typeController.selectedIndex = 1;
        List_Hero:RemoveChildrenToPool();
        for k, v in pairs(serverData) do
            local itemValue = {};
            self.heroData = logicMgr.HeroManager.getHeroDatabyModelId(v.modelId, true);
            local heroconfig = G.dataTable["t_hero"][tostring(v.modelId)];
            itemValue.star = heroconfig.f_HeroInitialStar;
            itemValue.icon = self.heroData.icon;
            if tonumber(itemValue.star) >= 20 and self.heroData.partnerId == 0 then
                itemValue.new = true;
            end
            itemValue.showSSR = itemValue.star >= 60;
            local item = List_Hero:AddItemFromPool()
            if self.HeadRectDrawItem ~= nil then
                self.HeadRectDrawItem.initData(item, itemValue);
            end
        end
    end
    if self.nowSelect == index then
        itemObj.selected = true;
    else
        itemObj.selected = false;
    end
end

function UISpecialDrawRecord:updateCost()
    local cost = decodeJsonStrToTabel(self.tableData.f_Cost);
    cost = cost[1];
    local haveNum = logicMgr.ItemManager.getBagNumByTableID(cost[1]);
    self.Loader_Cost.url = EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"][tostring(cost[1])].f_Icon;
    self.costName = HelperFunc.GetItemNameWithoutColor(getLanguage(G.dataTable["t_item"][tostring(cost[1])].f_ItemName));
    if haveNum >= cost[2] then
        self.costEnough = true;
        self.Text_Num.text = "[color=#FFFFFF]" .. cost[2] .. "[/color]";
    else
        self.costEnough = nil;
        self.Text_Num.text = "[color=#FF0000]" .. cost[2] .. "[/color]";
    end
end

--- 选中
function UISpecialDrawRecord:ClickTag(context)
    self.nowSelect = tonumber(context.data.name);
end

--- 纪录
function UISpecialDrawRecord:OnRecord()
    if self.serverData.param.selectDrawBox[tostring(self.nowSelect)] == nil then
        drawHandler.GoReqSelectDrawSaveMessage(self.activeId, tonumber(self.nowSelect));
    else
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
        pop.Init("31", function(b)
            if b then
                drawHandler.GoReqSelectDrawSaveMessage(self.activeId, tonumber(self.nowSelect));

            end
        end, self.nowSelect);
    end
end

--- 购买
function UISpecialDrawRecord:OnBuy()
    if self.nowSelect == nil then
        return ;
    end
    if not self.costEnough then
        UISysTips.AddMsg(string.format(getLanguage("Character_Tips19", nil, "%s"), self.costName));
        return;
    end
    local cost = decodeJsonStrToTabel(self.tableData.f_Cost);
    local resUrl = string.format(getLanguage("CommonPop_IconSetting"),cost[1][1] or 1)
    local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
    pop.Init("32", function(b)
        if b then
            if self.serverData.activeState==SpecialDrawState.buy then
                drawHandler.GoReqSelectDrawBuyMessage(self.activeId, tonumber(self.buyNumList[tonumber(self.nowSelect)]))
            else
                UISysTips.AddMsg(getLanguage( "ActivityDesc1040101"))
            end
        end
    end,self.nowSelect);
end

-- 关闭
function UISpecialDrawRecord:OnClose()
    if self.notClose then
        self.notClose = false;
        return ;
    end
    self:OnBack();
end

-- 隐藏
function UISpecialDrawRecord:OnDisable()
end

-- 卸载
function UISpecialDrawRecord:OnDestroy()
    self:removeEvent();
end

return UISpecialDrawRecord